package com.cuotibao.zhuoxiao.basics;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;

import com.cuotibao.zhuoxiao.basics.utils.Density;
import com.cuotibao.zhuoxiao.basics.utils.permission.PermissionManager;

/**
 * @author hdx on 2019/5/6.
 */
public abstract class BaseCommonActivity extends AppCompatActivity implements View.OnClickListener {
    private ProgressDialog mProgressDlg;
    protected boolean loadLayoutFinish = false;

    /**
     * 给界面设置title
     *
     * @param title
     */
    public abstract void setTitle(String title);

    /**
     * 给界面设置title
     *
     * @param title
     */
    public abstract void setTitle(int title);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isTaskRoot()
                && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
                && getIntent().getAction() != null
                && getIntent().getAction().equals(Intent.ACTION_MAIN)) {
            finish();
            return;
        }
        Density.setDensity(getApplication(), this);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.theme_green));
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadLayoutFinish = true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return true;
    }

    @SuppressLint("NewApi")
    public void showProgressDialog(String message) {
        if (mProgressDlg == null && !isFinishing() && !isDestroyed()) {
            mProgressDlg = new ProgressDialog(this);
            mProgressDlg.setMessage(message);
            mProgressDlg.setCanceledOnTouchOutside(false);
            // mProgressDlg.setCanceledOnTouchOutside(false);
            // mProgressDlg.setCancelable(false);
            mProgressDlg.show();
        }
    }


    @SuppressLint("NewApi")
    public void setProgressDialogMessage(String message) {
        if (mProgressDlg != null && mProgressDlg.isShowing()) {
            Context context = ((ContextWrapper) mProgressDlg.getContext()).getBaseContext();
            if (context instanceof Activity) {
                if (!((Activity) context).isFinishing() && !((Activity) context).isDestroyed())
                    mProgressDlg.setMessage(message);
            } else {
                mProgressDlg.setMessage(message);
            }
        }
    }

    @SuppressLint("NewApi")
    public void dismissProgressDialog() {
        if (mProgressDlg != null && mProgressDlg.isShowing()) {
            Context context = ((ContextWrapper) mProgressDlg.getContext()).getBaseContext();
            if (context instanceof Activity) {
                if (!((Activity) context).isFinishing() && !((Activity) context).isDestroyed())
                    mProgressDlg.dismiss();
            } else {
                mProgressDlg.dismiss();
            }
        }
        mProgressDlg = null;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.getInstances().onRequestPermissionsResult(requestCode, grantResults);
    }


    @Override
    protected void onDestroy() {
        dismissProgressDialog();
        PermissionManager.getInstances().onDestroy();
        super.onDestroy();
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }


    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        if (resources != null && resources.getConfiguration().fontScale != 1.0f) {
            Configuration configuration = resources.getConfiguration();
            configuration.fontScale = 1.0f;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        return resources;
    }


    //判断Activity是否Destroy
    public static boolean isDestroy(Activity activity) {
        if (activity == null || activity.isFinishing() || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed())) {
            return true;
        } else {
            return false;
        }
    }
}
